0% completed
Problem Statement
Given two integers, n and k, return the kth smallest factor of n. If n has fewer factors than k factors, return -1.
A factor of a number is an integer that can divide that number without leaving a remainder.
Examples
-
Example 1:
- Input:
n = 12,k = 5 - Expected Output:
6 - Justification: The factors of 12 are 1, 2, 3, 4, 6, and 12. The fifth smallest factor is 6.
- Input:
-
Example 2:
- Input:
n = 17,k = 2 - Expected Output:
17 - Justification: The factors of 17 are 1 and 17, since 17 is a prime number. The second smallest factor is 17.
- Input:
-
Example 3:
- Input:
n = 10,k = 5 - Expected Output:
-1 - Justification: The factors of 10 are 1, 2, and 5. 10 has only 3 factors, so the answer is
-1.
- Input:
Solution
To solve this problem, we will first calculate all the factors of n by iterating through numbers from 1 to n and checking if n is divisible by each of these numbers without a remainder. If a number is a factor, we increase the count of factors found so far. If the current factor is the kth smallest factor, we consider it as the answer.
This method is effective because it guarantees that we examine all potential divisors of n, thereby not missing any factors. Although this method may not be the most efficient for very large numbers due to its linear complexity, it is straightforward and reliable for a wide range of inputs.
Step-by-Step Algorithm
-
Initialize a counter: Start with a variable
factorCountset to 0. This counter will keep track of how many factors ofnwe have found. -
Iterate through potential factors: Loop through numbers
istarting from 1 up toninclusive. Eachirepresents a potential factor ofn. -
Check for divisibility: For each
i, check ifn % i == 0. This checks whetheriis a factor ofnby seeing if the division leaves no remainder. -
Increment the factor counter: If
iis a factor ofn(i.e., the condition in step 3 is true), incrementfactorCountby 1. -
Check if the current factor is the kth factor: If
factorCountequalskat any point, returni. This means we've found thekthfactor ofn. -
Finish the loop without finding the kth factor: If the loop completes and
factorCountis less thank, return -1. This indicates thatndoes not have akthfactor.
Algorithm Walkthrough
Let's consider the Input: n = 12, k = 5
-
Step 1: Initialize
factorCountto 0. -
Step 2: Start iterating from
i = 1toi = 12(inclusive).- When
i = 1,12 % 1 == 0. Factor found. IncrementfactorCountto 1. - When
i = 2,12 % 2 == 0. Factor found. IncrementfactorCountto 2. - When
i = 3,12 % 3 == 0. Factor found. IncrementfactorCountto 3. - When
i = 4,12 % 4 == 0. Factor found. IncrementfactorCountto 4. - When
i = 5,12 % 5 != 0. No action is taken since 5 is not a factor. - When
i = 6,12 % 6 == 0. Factor found. IncrementfactorCountto 5.
- When
-
Step 5: At this point, when
i = 6,factorCountequalsk(which is 5). This means we've found the 5th factor of 12. Returni, which is 6. -
The algorithm returns 6 as the output, indicating that the 5th smallest factor of 12 is 6.
Code
Complexity Analysis
Time Complexity:
The primary operation in this algorithm involves iterating through all numbers from 1 to n to find all factors of n. This iteration constitutes the bulk of the computational work, leading to a time complexity of On is the input number.
Space Complexity:
The algorithm uses the constant space. So, the space complexity is
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity:
Space Complexity: